package cn.stylefeng.guns.modular.order.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.config.util.LogisticsUtil;
import cn.stylefeng.guns.modular.order.model.ExpressCompany;
import cn.stylefeng.guns.modular.order.model.Order;
import cn.stylefeng.guns.modular.order.model.OrderVerificationCode;
import cn.stylefeng.guns.modular.order.service.IExpressCompanyService;
import cn.stylefeng.guns.modular.order.service.IOrderService;
import cn.stylefeng.guns.modular.order.service.IOrderVerificationCodeService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 杨煜
 * @Date: 2019/12/11 14:11
 * @Description:
 */
@RestController
@RequestMapping("/rest/order")
public class RestOrderController  extends BaseController {

    @Autowired
    private IOrderVerificationCodeService orderVerificationCodeService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IExpressCompanyService expressCompanyService;

    /**
     * 获取order列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Integer userId) {
        Page<Map<String, Object>> orders = orderService.selectOrders(userId);
        return ResponseData.success(orders);
    }


    /**
     * 新增order
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Order order) {
        if(!orderService.saveOrder(order)){
            return ResponseData.error("保存失败！");
        }
        return SUCCESS_TIP;
    }

    /**
     * 获取order列表
     */
    @RequestMapping(value = "/verify")
    @ResponseBody
    public ResponseData verify(String code) {
        OrderVerificationCode bean = orderVerificationCodeService.verifyCode(code);
        if(null == bean){
        }else{
            return ResponseData.success();
        }
        return ResponseData.error("验证码无效");
    }


    /**
     * 查询订单的物流信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/logistics")
    @ResponseBody
    public ResponseData logistics(Integer id) {
        Order order = orderService.getById(id);
        if(null == order){
            return ResponseData.error("订单不存在");
        }
        if(null == order.getExpressCompanyId() || StringUtils.isEmpty(order.getTrackingNo())){
            return ResponseData.error("物流信息不全");
        }
        //物流公司code
        ExpressCompany expressCompany = expressCompanyService.getById(order.getExpressCompanyId());
        if(null == expressCompany){
            return ResponseData.error("物流信息不全");
        }
        LogisticsUtil api = new LogisticsUtil();
        try {
            return ResponseData.success(api.getOrderTracesByJson(expressCompany.getCode(), order.getTrackingNo()));
        } catch (Exception e) {
            return ResponseData.error("查询失败");
        }
    }

}
